/*
   Copyright 2011 
   
   DIFED

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package com.difed.cccam.api.parseadores;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.difed.cccam.entidades.Cliente;

public final class ParseadorClientes {
    
    private static final String INICIO_CABECERA_1 = "| Username";
    private static final String INICIO_CABECERA_2 = "+-----";
    private static final String PATRON_ECM = "(\\d+) \\((\\d+)\\)";
    private static final String PATRON_SHARE = "([^\\|]+) \\((n?ok)\\)";
    
    private ParseadorClientes() {
        
    }
    
    public static List<Cliente> parsea(String result) throws IOException {
        List<Cliente> clientes = new ArrayList<Cliente>();        
        BufferedReader reader = new BufferedReader(new StringReader(result));
        int estado = 0;
        String[] valores;
        Cliente cliente = null;
        String linea = reader.readLine();
        while (linea != null) {
            switch (estado) {
            case 0:
                if (linea.startsWith(INICIO_CABECERA_1)) {
                    estado++;
                }
                break;
            case 1:
                if (linea.startsWith(INICIO_CABECERA_2)) {
                    estado++;
                }
                break;
            case 2:
                if (linea.startsWith("|")) {
                    valores = linea.substring(1).split("\\|");
                    cliente = creaCliente(valores);
                    clientes.add(cliente);
                } else {
                    estado++;                        
                }
                break;
            }
            if (estado < 3) {
                linea = reader.readLine();
            } else {
                break;
            }
        }
        return clientes;
    }
    
    private static Cliente creaCliente(String[] valores) {
        Cliente cliente = new Cliente();
        cliente.setUserName(valores[0].trim());
        cliente.setIp(valores[1].trim());
        cliente.setConectado(true);
        cliente.setTiempoInactivo(valores[3].trim());
        Pattern pattern = Pattern.compile(PATRON_ECM);
        Matcher matcher = pattern.matcher(valores[4]);
        if (matcher.find()) {
            cliente.setEcmTotal(Integer.parseInt(matcher.group(1)));
            cliente.setEcmValidas(Integer.parseInt(matcher.group(2)));
        }
        cliente.setVersionCcam(valores[6].trim());
        pattern = Pattern.compile(PATRON_SHARE);
        matcher = pattern.matcher(valores[7]);
        if (matcher.find()) {
            cliente.setUltimoShared(matcher.group(1));
            cliente.setSharedValido(matcher.group(2).equals("ok"));
        }
        return cliente;
    }

}
